﻿using shop.Bll;
using shop.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using unvell.ReoGrid;
using shop.Bll.Logi;
using CommonTools;
using CommonTools.Controls;

namespace shop.Logi
{
    public partial class LogiFeeReportForm : DockContent
    {

        shop.Bll.LogisticsFeeConfigBll _bll = new shop.Bll.LogisticsFeeConfigBll();


        public LogiFeeReportForm()
        {
            InitializeComponent();
            this.box_begin.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            this.box_end.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month));
        }

        private async void btn_Query_Click(object sender, EventArgs e)
        {
            var begin = box_begin.Value.Value;
            var end = box_end.Value.Value;

            var res = await WaitUI.RunAsync(async () =>
            {
                return (await _bll.QueryFeeReport2(begin, end, false)).Item1;
            });


            new GridDataBuilder(this.grid)
                .UseData(res)
                .AutoCreateColumns()
                .UseFilter()
                .End();

            if (res == null || res.Rows.Count== 0)
            {
                MessageBox.Show("没有查询到数据", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        /// <summary> 导出明细 </summary>
        private void btn_export_Click(object sender, EventArgs e)
        {
            //明细就是出库加上结算运费结算重量,是否预估,

            var begin = box_begin.Value.Value;
            var end = box_end.Value.Value;

            var res = WaitUI.Run(async () =>
            {
                //return await _bll.QueryFeeReportDetails(begin, end, true);
                return await _bll.QueryFeeReport2(begin, end, true);
            });

            new ExcelExporter()
                .NewSheet("网店销售")
                .AddDataTable(res.Item2)
                .End(null);


            // 
            // 物流单号	货品数量	订单编号	原始单号	订单类型	仓库	店铺	商家编码	收货地区	物流公司	快递老板	客服备注	发货时间	结算运费	结算重量

            //new ExcelExporter()
            //    .NewSheet("网店销售")
            //    .AddList(res)
            //    .AddColumns(new Dictionary<string, string>
            //    {
            //        {nameof(LogiFeeReport.logistics_no),"物流单号" },
            //        {nameof(LogiFeeReport.num),"货品数量" },
            //        {nameof(LogiFeeReport.trade_no),"订单编号" },
            //        {nameof(LogiFeeReport.src_tids),"原始单号" },
            //        {nameof(LogiFeeReport.trade_type),"订单类型" },
            //        {nameof(LogiFeeReport.warehouse),"仓库" },
            //        {nameof(LogiFeeReport.shop_name),"店铺" },
            //        {nameof(LogiFeeReport.spec_no),"商家编码" },
            //        {nameof(LogiFeeReport.rece_province),"收货地区" },
            //        {nameof(LogiFeeReport.logistics_name),"物流公司" },
            //        {nameof(LogiFeeReport.LogiAcct),"快递老板" },
            //        {nameof(LogiFeeReport.cs_remark),"客服备注" },
            //        {nameof(LogiFeeReport.consign_time),"发货时间" },
            //        {nameof(LogiFeeReport.post_amount),"结算运费" },
            //        {nameof(LogiFeeReport.weight),"结算重量" },
            //        {nameof(LogiFeeReport.ForecastAmount),"预估运费" },
            //        {nameof(LogiFeeReport.IsForecastText),"是否预估" },
            //        {nameof(LogiFeeReport.NoLogiTraceText),"无快递记录" },
            //        {nameof(LogiFeeReport.NotInOutstockText),"无出库信息" },
            //    })
            //    .SetColumnFormat(12,"yyyy-MM-dd HH:mm:ss")
            //    //.SetBoolText("是", "否")
            //    .End(null);


        }

        private async void btn_QueryNoFee_Click(object sender, EventArgs e)
        {
            var begin = box_begin.Value.Value;
            var end = box_end.Value.Value;

            var res = await WaitUI.RunAsync(async () =>
            {
                return await _bll.QueryQueryNoBill(begin, end);
            });



            //new GridDataBuilder(this.grid)
            //    .UseData(res)
            //    .AutoCreateColumns()
            //    .UseFilter()
                

            if (res == null || res.Any() == false)
            {
                MessageBox.Show("没有查询到数据", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {

                new ExcelExporter()
                    .NewSheet(null)
                    .AddDataTable(res.ToDataTable())
                    .EndThenOpen(null);

                MessageTip.ShowOk(this, "查询完成");
            }
        }
    }
}
